<template>
  <div class="padding-l-20 padding-r-20 padding-t-20 h-100 table-page">
    <filter-bar>
      <template slot="left-bar">
        <a-space>
          <a-input class="position-r" placeholder="请输入搜索"
            v-model="searchForm.appName" />
            <a-button class="nbcs-btn-search" @click="page.currentPage = 1;getList(page, searchForm)">查询</a-button>
            <a-button class="nbcs-btn-default" @click="clearObj(searchForm)">重置</a-button>
        </a-space>
      </template>
    </filter-bar>
    <interval-bg></interval-bg>
    <opt-bar>
      <div slot="left-bar">
        应用管理
      </div>
      <div slot="right-bar">
        <a-space>
          <a-button @click="delAll()" class="nbcs-btn-default" style="top:-1.6px" v-if="permission('sys:applicationInfo:delete')">删除应用</a-button>
          <a-button @click="edit(0)" class="nbcs-btn-main-bg" style="top:-1.6px" v-if="permission('sys:applicationInfo:add')">新增应用</a-button>
        </a-space>
      </div>
    </opt-bar>
    <a-table ref="_table" bordered :columns="columns" :data-source="list" :pagination="pageConfig" rowKey="id" :loading="loading"
      :scroll="{ x: 1000, y: tableHeight }" :row-selection="rowSelection">
      <template slot="roles" slot-scope="text">
        <a-tag v-for="(item, index) in text" :key="index">{{ item.name }}</a-tag>
      </template>
      <span slot="appStatus" slot-scope="text, record">
        <a-tag v-if="record.appStatus" class="tag-green">
          启用
        </a-tag>
        <a-tag v-else class="tag-red">
          禁用
        </a-tag>
      </span>
      <span slot="action" slot-scope="text, record">
        <a @click="edit(1, record)" v-if="permission('sys:applicationInfo:update')">编辑</a>
        <a-divider type="vertical" v-if="permission('sys:applicationInfo:update') && permission('sys:applicationInfo:query')"/>
        <a @click="edit(2, record)" v-if="permission('sys:applicationInfo:query')">查看</a>
      </span>
    </a-table>
    <application-info-edit ref="applicationInfoEdit" @getList="getList(page, searchForm)"></application-info-edit>
  </div>
</template>

<script>
import FilterBar from "@/layouts/FilterBar.vue";
import IntervalBg from "@/layouts/IntervalBg.vue";
import OptBar from "@/layouts/OptBar.vue";
import { list, deleted } from "@/services/applicationInfo";
import { deleteList } from "@/services/applicationInfo";
import ApplicationInfoEdit from "./applicationInfoEdit";
import {clearObj} from "../../utils/util";
let expandedRowKeys = new Array();
export default {
  name: "WordList",
  components: { FilterBar, ApplicationInfoEdit, IntervalBg, OptBar },
  data() {
    return {
      tableHeight: 0,
      loading: false,
      searchForm: {
        word: "",
      },
      list: [],
      columns: [
        { title: "应用名称", dataIndex: "appName", scopedSlots: { customRender: 'appName' }, },
        {
          title: "状态",
          dataIndex: "appStatus",
          scopedSlots: { customRender: 'appStatus' },
        },
        {
          title: "操作",
          key: "operation",
          align: 'center',
          fixed: "right",
          width: 140,
          scopedSlots: { customRender: "action" }
        }
      ],
      pageConfig: {
        showQuickJumper: true,
        showSizeChanger: true,
        hideOnSinglePage: false, //只有一页时是否隐藏分页器
        defaultPageSize: 20,
        pageSizeOptions: ["20", "30", "50"],
        total: 0,
        current: 1,
        showTotal: total => {
          return `共 ${total} 条`;
        },
        onShowSizeChange: (page, pageSize) => {
          this.page.currentPage = 1;
          this.page.pageSize = pageSize;
          this.getList(this.page, this.searchForm);
        },
        onChange: (page) => {
          this.page.currentPage = page;
          this.pageConfig.current = page;
          this.getList(this.page, this.searchForm);
        },
      },
      page: {
        currentPage: 1,
        pageSize: 20
      },
      rowSelection: {
        onChange(selectedRowKeys) { expandedRowKeys = selectedRowKeys },
        onSelect() {}
      }
    };
  },
  computed: {},
  created() {
    this.getList(this.page);
  },
  mounted() {
      this.tableHeight = document.querySelector('.table-page').clientHeight - 276;
  },
  methods: {
    getList(page, params) {
      this.loading = true;
      list(
        Object.assign(
          {
            page: page.currentPage,
            pageSize: page.pageSize,
          },
          params
        )
      ).then(res => {
        this.loading = false;
        const { data } = res.data;
        this.list = data.list;
        this.pageConfig.current = data.page;
        this.pageConfig.total = data.totalRow;
      });
    },
    ch(selectedRowKeys) {
      console.log(selectedRowKeys)
    },
    delAll() {
      if(expandedRowKeys.length == 0) {
        this.$message.warn("请选择应用");
        return;
      }
      deleteList({ idList: expandedRowKeys }).then(res => {
        if (res.data.code === 0) {
          this.$message.success("删除成功");
          this.getList(this.page);
          expandedRowKeys = [];
        } else {
          this.$message.error(res.message);
        }
      });
    },

    del(obj) {
      deleted({ id: obj.id }).then(res => {
        if (res.data.code === 0) {
          this.$message.success("删除成功");
          this.getList(this.page);
        } else {
          this.$message.error(res.message);
        }
      });
    },
    //添加/编辑
    edit(action, data = {}) {
      this.$refs['applicationInfoEdit'].changeForm(true, action, data);
    },
    clearObj,
    permission(name) {
      let permissions = this.$store.getters["account/permissions"];
      return permissions.filter(m => m.id == name || m.operation.indexOf(name) >= 0).length > 0;
    }
  }
};
</script>

<style lang="less" scoped>
.select {
  width: 170px;

  /deep/ .ant-select-selection {
    width: 100%;
  }
}

._input {
  width: 180px;
}

.table-page {
  .ant-table-wrapper {
    max-height: calc(100% - 150px);
  }
}
</style>
